using System;
using System.Xml;

namespace FridayShopping.DataModel
{
	/// <summary>
	/// Abstract class that represents a single item of data in a shopping list
	/// data model
	/// </summary>
	public abstract class ItemBase : IComparable
	{
		private Key		key;
		private string	name;

		#region Constructors
		public ItemBase()
		{
		}

		public ItemBase(string name)
		{
			this.name = name;
		}
		#endregion

		#region Member Access
		public Key Key
		{
			get
			{				
				return key;
			}
			set
			{
				key = value;
			}
		}

		public string Name
		{
			get
			{
				return name;
			}
			set
			{
				name = value;
			}
		}
		#endregion

		#region IComparable Implementation
		public int CompareTo(object obj)
		{
			return Name.CompareTo(((ItemBase)obj).Name);
		}
		#endregion

		#region Serialization/Deserialization

		public void Serialize(XmlTextWriter xmlWriter)
		{
			xmlWriter.WriteStartElement(GetType().Name);

			SerializeMembers(xmlWriter);

			xmlWriter.WriteEndElement();
		}

		virtual protected void SerializeMembers(XmlTextWriter xmlWriter)
		{
			Key.Serialize(xmlWriter);

			xmlWriter.WriteElementString("Name", Name);
		}

		virtual public void DeserializeMembers(XmlNodeReader xmlReader)
		{
			// Read key
			Key = Key.DeserializeMembers(xmlReader);			
			// Read Name
			Name = xmlReader.ReadElementString();
		}
		#endregion

		public override string ToString()
		{
			return Name.ToString();
		}
	}

	/// <summary>
	/// Attribute attached to a list of items describing the type of each member
	/// of the list. Used because there is no way to create instance from a string
	/// in the compact framework. (maybe in the future?)
	/// </summary>
	public class ListItemTypeAttribute : Attribute 
	{
		private Type itemType;

		#region Constructor
		public ListItemTypeAttribute(Type itemType)
		{
			this.itemType = itemType;
		}
		#endregion

		#region Member Access
		public Type ItemType
		{
			get
			{
				return itemType;
			}
		}
		#endregion
	}
}
